package top.opm.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.jx.file.FileService;
import top.jx.file.FileTypeConstant;
import top.jx.file.FileUtil;
import top.jx.model.FileInfo;
import top.jx.service.SambaService;
import top.opm.config.FileServiceConfig;
import top.opm.dao.HotfixFileDao;
import top.opm.entity.HotfixFile;
import top.opm.model.ResultData;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping( value = "/hotFix")
@Api(value = "补丁包维护接口")
public class HotfixFileUploadController {

    @Autowired
    private HotfixFileDao hotfixFileDao;
    @Autowired
    private SambaService sambaService;
    @Autowired
    private FileServiceConfig fileServiceConfig;
    @Autowired
    private FileService fileService;

    @PostMapping(value = "/uploadPatch")
    @ApiOperation(value="补丁包上传", notes="上传需要部署的文件")
//    @ApiImplicitParam(paramType="query", name = "file", value = "hotfix", required = true, dataType = "file",defaultValue = "file")
    @ApiParam(value="上传文件", required = true)
    public ResultData uploadPatch(@RequestPart("file") MultipartFile file){
        String fileName = file.getOriginalFilename();

        HotfixFile HotfixFile = new HotfixFile();
        File tempFile = fileService.getTempFile(FileUtil.getFileSuffix(fileName));
        try {
            file.transferTo(tempFile);
            FileInfo upload = null;
            if (fileServiceConfig.getMode() == 1) {
                upload = sambaService.upload(tempFile, fileName, FileTypeConstant.hotfix);
            } else {
                fileService.saveLocalPath(tempFile, FileTypeConstant.hotfix);
            }
            HotfixFile.setName(fileName);
            HotfixFile.setPath(upload.getPath());
            HotfixFile.setType(FileTypeConstant.hotfix);
            hotfixFileDao.save(HotfixFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ResultData.success();
    }
}
